package org.ninjarope.cinderstained.main.gameplay.definition.pool.implement;

import java.util.HashMap;

import org.ninjarope.cinderstained.main.engine.Game;
import org.ninjarope.cinderstained.main.gameplay.definition.pool.Pool;
import org.ninjarope.cinderstained.main.gameplay.definition.pool.PoolMember;

public class DefinitionPool<T extends PoolMember<T>> {
	public HashMap<String, T> def;
	protected Pool<T> objects;

	DefinitionPool(String pool) {		
		objects = Game.pools(pool);
		def = new HashMap<String, T>();

		// ukoliko nije pronadjen dati pool baci exception
		if (objects == null) {
			throw new RuntimeException();
		}
	}

	/**
	 * Metoda za kreiranje objekta na osnovu definicije
	 * 
	 * @param definition
	 *            - naziv definicije ciju instancu zelimo da kreiramo
	 * @return instanca definicije
	 */
	public T request(String definition) {
		T temp = def.get(definition);
		if (temp != null) {
			T stuff = objects.request();
			if (stuff != null)
				return stuff.assimilate(temp);
			else
				return null;
		} else
			return null;
	}

	/**
	 * Metoda za vracanje objekta nazad u pool
	 * 
	 * @param t
	 *            - objekat
	 */
	public void release(T t) {
		objects.release(t);
	}
}
